package com.sk.cardeal.act.settlement;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.sk.cardeal.R;
import com.sk.cardeal.adapter.SettlementCashAdapter;
import com.sk.cardeal.base.BaseActivity;
import com.sk.cardeal.callback.CashPayCallback;
import com.sk.cardeal.callback.HandleDataCallBack;
import com.sk.cardeal.dialog.BTPayTipDialogFragment;
import com.sk.cardeal.dialog.CashPayDialogFragment;
import com.sk.cardeal.entity.PayEntity;
import com.sk.cardeal.entity.PayResult;
import com.sk.cardeal.entity.WorkFormDetails;
import com.sk.cardeal.utils.AppApi;
import com.sk.cardeal.utils.MyUtils;
import com.sk.cardeal.utils.SnackUtils;
import com.sk.cardeal.view.TitleView;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by sk on 2018/4/16.
 */

public class SettlementActivity extends BaseActivity implements HandleDataCallBack, CashPayCallback {

    @BindView(R.id.title_view)
    TitleView titleView;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.car_num)
    TextView carNum;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.fix_type)
    TextView fixType;
    @BindView(R.id.cash_pay)
    TextView cashPay;
    @BindView(R.id.wx_pay)
    TextView wxPay;
    @BindView(R.id.alipay)
    TextView alipay;
    @BindView(R.id.tv_bt_tip)
    TextView tvBtTip;
    @BindView(R.id.bt_pay)
    RelativeLayout btPay;
    @BindView(R.id.data_ryc)
    RecyclerView dataRyc;
    private WorkFormDetails workFormDetails;
    private SettlementCashAdapter adapter;
    private LoadingDialog loadingDialog;
    private String orderId = "";
    PayEntity payEntity;
    private double white_m = 0;
    private double total_m = 0;

    @Override
    protected int getContentView() {
        return R.layout.activity_settlement;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleView.setMyCallBack(this);
        loadingDialog = MyUtils.getLoadingDialog(mContext, false);
        payEntity = new PayEntity();
        String data = getIntent().getStringExtra("data");
        WorkFormDetails workFormDetails = JSON.parseObject(data, WorkFormDetails.class);
        List<WorkFormDetails.ReturnObjectBean.RepairOrderDetailListBean> datalist = workFormDetails.getReturnObject().getRepairOrderDetailList();
        adapter = new SettlementCashAdapter(datalist);
        dataRyc.setLayoutManager(new LinearLayoutManager(mContext));
        dataRyc.setAdapter(adapter);

        orderId = workFormDetails.getReturnObject().getId();
        payEntity.setOrderid(orderId);

        tvPrice.setText(String.valueOf(workFormDetails.getReturnObject().getTotalPrice()));
        white_m = workFormDetails.getReturnObject().getWhite_money();
        total_m = workFormDetails.getReturnObject().getTotalPrice();

        if (workFormDetails.getReturnObject().getWhite_money() > 0) {
            tvBtTip.setText("白条剩余额度:" + workFormDetails.getReturnObject().getWhite_money());
        } else {
            tvBtTip.setText("暂无白条额度");
        }
        tvCode.setText("工单号：" + workFormDetails.getReturnObject().getCode());
        tvTime.setText(workFormDetails.getReturnObject().getCreateTime());
        carNum.setText(workFormDetails.getReturnObject().getCarNumber());
        fixType.setText(workFormDetails.getReturnObject().getRepairName());

    }

    @Override
    public void clickTitle(int i) {
        super.clickTitle(i);
        switch (i){
            case 1:
                finish();
                break;
        }
  }

    @OnClick({R.id.cash_pay, R.id.wx_pay, R.id.alipay, R.id.bt_pay})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cash_pay:

                CashPayDialogFragment cashPayDialogFragment = new CashPayDialogFragment();
                cashPayDialogFragment.setCashPayCallback(SettlementActivity.this);
                cashPayDialogFragment.show(getSupportFragmentManager(), "cash");

                break;
            case R.id.wx_pay:
                loadingDialog.show();
                payEntity.setPaymoney(-1);
                payEntity.setUserid(MyUtils.getUser(mContext).getId());
                payEntity.setPaytype(2);
                AppApi.payForOrder(payEntity, SettlementActivity.this, 2, SettlementActivity.this);
                break;
            case R.id.alipay:
                loadingDialog.show();
                payEntity.setPaymoney(-1);
                payEntity.setUserid(MyUtils.getUser(mContext).getId());
                payEntity.setPaytype(3);
                AppApi.payForOrder(payEntity, SettlementActivity.this, 2, SettlementActivity.this);
                break;
            case R.id.bt_pay:

                if(white_m<=0){
                    SnackUtils.showError(mRootView,"该用户暂无白条额度！");
                    return;
                }

                if(white_m<total_m){
                    SnackUtils.showError(mRootView,"该用户白条额度不够支付次订单！");
                    return;
                }

                BTPayTipDialogFragment btPayTipDialogFragment = new BTPayTipDialogFragment();
                btPayTipDialogFragment.setMyCallBack(SettlementActivity.this);
                btPayTipDialogFragment.show(getSupportFragmentManager(), "bt");

                break;
        }
    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        loadingDialog.cancelDialog();
        PayResult payResult = JSON.parseObject(result, PayResult.class);
        switch (tag) {
            case 1:
                Intent cashIntent = new Intent(mContext, CashSuccessActivity.class);
                startActivity(cashIntent);
                break;
            case 2:
                String wxdata = payResult.getReturnObject();
                Intent intent = new Intent(mContext, WxPayActivity.class);
                intent.putExtra("data", wxdata);
                startActivity(intent);
                break;
            case 3:
                String alidata = payResult.getReturnObject();
                Intent aliIntent = new Intent(mContext, AliPayActivity.class);
                aliIntent.putExtra("data", alidata);
                startActivity(aliIntent);
                break;
            case 4:

                break;
        }

    }

    @Override
    public void handlerFailData(String failString, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public void handleErrorData(String error, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public void cashPay(String price) {
        loadingDialog.show();
        payEntity.setPaymoney(Double.parseDouble(price));
        payEntity.setUserid(MyUtils.getUser(mContext).getId());
        payEntity.setPaytype(5);
        AppApi.payForOrder(payEntity, SettlementActivity.this, 1, SettlementActivity.this);
    }

    @Override
    public void sure() {
        super.sure();
        loadingDialog.show();
        payEntity.setPaymoney(-1);
        payEntity.setUserid(MyUtils.getUser(mContext).getId());
        payEntity.setPaytype(4);
        AppApi.payForOrder(payEntity, SettlementActivity.this, 1, SettlementActivity.this);
    }
}
